package com.woniuxy.caradmin.controller;


import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.woniuxy.caradmin.entity.pojo.Driver;
import com.woniuxy.caradmin.entity.vo.DriverVo;
import com.woniuxy.caradmin.exception.CarException;
import com.woniuxy.caradmin.service.DriverService;
import com.woniuxy.caradmin.util.Constaint;
import com.woniuxy.caradmin.util.DataGridView;
import com.woniuxy.caradmin.util.R;
import com.woniuxy.caradmin.util.ResultCode;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author womenzu
 * @since 2020-12-07
 */
@RestController
@RequestMapping("/driver")
public class DriverController {
    @Autowired
    private DriverService driverService;

    @GetMapping("/selectAllDriver")
    @ApiOperation("查询驾驶员档案")
    public R selectAllDriver(){
        List<Driver> drivers = driverService.selectAllDriver();
        if(drivers!=null && drivers.size()>0){
            return R.ok().code(ResultCode.DRIVERISNOTNULL).message(Constaint.DRIVERISNOTNULL)
                    .data("drivers",drivers);
        }else{
            throw new CarException(ResultCode.DRIVERISNULL,Constaint.DRIVERISNULL);
        }
    }
    @GetMapping("/selectDriverById")
    @ApiOperation("id查询驾驶员档案")
    public R selectDriverById(Integer  id) {
        Driver drivers = driverService.selectAllDriverByid(id);
        if(drivers!=null){
            return R.ok().code(ResultCode.DRIVERISNOTNULL).message(Constaint.DRIVERISNOTNULL)
                    .data("drivers",drivers);
        }else{
            throw new CarException(ResultCode.DRIVERISNULL,Constaint.DRIVERISNULL);
        }
    }
    @GetMapping("/selectAllDriver1")
    @ApiOperation("查询驾驶员档案")
    public R selectAllDriver1(){
        List<Driver> drivers = driverService.selectAllDriver1();
        if(drivers!=null && drivers.size()>0){
            return R.ok().code(ResultCode.DRIVERISNOTNULL).message(Constaint.DRIVERISNOTNULL)
                    .data("drivers",drivers);
        }else{
            throw new CarException(ResultCode.DRIVERISNULL,Constaint.DRIVERISNULL);
        }
    }

    @PutMapping("/delectDriver")
    @ApiOperation("删除驾驶员档案")
    public R delectDriver(Integer id) {
        Integer integer = driverService.delectDriver(id);
        if (integer > 0) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        } else {
            throw new CarException(ResultCode.ERROR, Constaint.ERROR);
        }
    }

    @PutMapping("/updateDriver")
    @ApiOperation("修改驾驶员档案")
    public R updateDriver(Driver driver) {
        Integer integer = driverService.updateDriver(driver);
        if (integer > 0) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        } else {
            throw new CarException(ResultCode.ERROR, Constaint.ERROR);
        }
    }

    @PostMapping("/insertDriver")
    @ApiOperation("增加驾驶员档案")
    public R insertDriver(Driver driver) {
        Integer integer = driverService.insertDriver(driver);
        if (integer > 0) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        } else {
            throw new CarException(ResultCode.ERROR, Constaint.ERROR);
        }
    }

    @GetMapping("/driverList")
    @ApiOperation("查询加分页")
    public DataGridView driverList(DriverVo dirverVo) {
        Page<Object> page = PageHelper.startPage(dirverVo.getPage(), dirverVo.getLimit());
        List<Driver> drivers = driverService.selectNameAndPho(dirverVo);
        return new DataGridView(page.getTotal(), drivers);

    }

    @ApiOperation("驾驶员批量删除")
    @PostMapping("/deleteAllPath")
    public R deleteAllPath(Integer[] ids) {
        System.out.println("kkkkk"+ids);
        boolean b = driverService.delectAllPathDriver(ids);
        if (b) {
            return R.ok().code(ResultCode.SUCCESS).message(Constaint.SUCCESS);
        } else {
            throw new CarException(ResultCode.ERROR, Constaint.ERROR);
        }
    }
}

